package com.vq3.digitalizationsoftwareintegrates.Utils.ExceptionHandler;

import com.vq3.digitalizationsoftwareintegrates.Entity.Info.ResultInfo;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.UserEntity;
import com.vq3.digitalizationsoftwareintegrates.Service.User.UserLoginService;

import com.vq3.digitalizationsoftwareintegrates.Servlet.WebSocket.WebSocketServer;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.Impl.PackagedDataImpl;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.PackagedData;
import lombok.SneakyThrows;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义认证成功之后的处理
 */
@Component
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Resource
    UserLoginService userLoginService;
    @Resource
    WebSocketServer webSocketServer;

    @SneakyThrows
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        PackagedData packagedData = new PackagedDataImpl();
        ResultInfo resultInfo = new ResultInfo();
        String name = authentication.getName();
        UserEntity userEntity = userLoginService.findUser(name);
        request.getSession().setAttribute("USER", userEntity);
        resultInfo.setFlag(true);
        packagedData.writeValue(resultInfo,response);


    }
}
